﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Omes.Model;

namespace Omes.Services
{
    public class ResourceService:IResourceService
    {
        public ImagePage GetImages(int pageid, string blockName=null, int pageNumber=0, int pageSize=0,int commend=0)
        {
            var ds = new DbServer();
            var imgall=ds.GetImageListOfPage(pageid);
            
            if (commend != 0)
            {
                if (commend == 1)
                {
                    imgall = imgall.Where(i => i.Commend.ToLower() == "true").ToList();
                }
                else
                {
                    imgall = imgall.Where(i => i.Commend.ToLower() == "false").ToList();
                }
            }
            var totalcount = imgall.Count;
            if (pageNumber != 0)
            {
                imgall = imgall.Skip(pageSize * (pageNumber - 1)).Take(pageSize).ToList();
            }
            var ip = new ImagePage();
            ip.Images = imgall;
            ip.TotalCount = totalcount;
            return ip;

        }
        public Html GetHtml(int pageid,string blockName=null,int commend=0)
        {
            var ds = new DbServer();
            var html = ds.GetPageHtml(pageid);
            return html;
        }
        public List<Link> GetLinks(int pageid, string blockName = null, int commend = 0)
        {
            return null;
        }
        public ListPage GetNews(int pageid, string blockName = null, int pageNumber = 0, int pageSize = 0, int commend = 0, string newType = null)
        {
            var ds = new DbServer();
            var newall= ds.GetPageNews(pageid);
            
            if (commend != 0)
            {
                if (commend == 1)
                {
                    newall = newall.Where(i => i.Commend.ToLower() == "true").ToList();
                }
                else
                {
                    newall = newall.Where(i => i.Commend.ToLower() == "false").ToList();
                }
            }
            if (!string.IsNullOrEmpty(newType))
            {
                newall = newall.Where(i => i.Kind.ToLower()==newType.ToLower()).ToList();
            }
            var totalcount = newall.Count;
            if (pageNumber != 0)
            {
                newall = newall.Skip(pageSize * (pageNumber - 1)).Take(pageSize).ToList();
            }
            var ip = new ListPage();
            if (newall != null)
            {
                foreach (var n in newall)
                {
                    var match = Regex.Match(n.Content, @"img[^>]*?src=['""](.+?)['""]");
                   if (match.Success)
                   {
                       n.BlockName = match.Groups[1].Value;
                   }
                   else
                   {
                       n.BlockName=System.Configuration.ConfigurationManager.AppSettings["NoImg"];
                   }
                }
            }

            ip.Lists = newall;
            ip.TotalCount = totalcount;
            return ip;
        }
        public Html GetHtmlByid(int htmlid)
        {
            var ds =new  DbServer();
            var html=ds.GetHtmlById(htmlid);
            return html;
        }
        public NewsList GetLinkByid(int linkid)
        {
             var ds = new DbServer();
             return ds.GetNewsInfo(linkid);
        }
        public ImageList GetImageByid(int imgid)
        {
            var ds = new DbServer();
            return ds.GetImage(imgid);
        }

        public List<Omes.Model.ImageList> GetAds()
        {
            var ds = new DbServer();
            return ds.GetAds();
        }
       
    }
}
